package com.glacier.common.core.constant;

import java.util.Arrays;
import java.util.Objects;

/**
 * date 2022-11-06 13:33
 * content-type 枚举
 *
 * @author glacier
 * @version 1.0
 */
public enum ContentTypeEnum {

    /**
     * 文件后缀名 对应 的content-type
     */
    HTM("htm", "text/html"),
    HTML("html", "text/html"),
    SHTML("htm", "text/html"),
    CSS("css", "text/css"),
    JS("js", "application/x-javascript"),
    XML("xml", "text/xml"),

    APK("apk", "application/vnd.android.package-archive"),
    EXE("exe", "application/x-msdownload"),
    MSI("msi", "application/x-msdownload"),
    /**
     * 图片
     */
    GIF("gif", "image/gif"),
    JPG("jpg", "image/jpeg"),
    JPEG("jpeg", "image/jpeg"),
    PNG("jpeg", "image/png"),
    TIF("tif", "image/tiff"),
    TIFF("tiff", "image/tiff"),
    ICO("ico", "image/x-icon"),
    JNG("jng", "image/x-jng"),
    BMP("bmp", "image/x-ms-bmp"),
    SVG("svg", "image/svg+xml"),
    /**
     * office
     */
    DOC("doc", "application/msword"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    XLS("xls", "application/vnd.ms-excel"),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    PPT("ppt", "application/vnd.ms-powerpoint"),
    PPTX("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),

    PDF("pdf", "application/pdf"),
    RTF("rtf", "application/rtf"),
    SWF("swf", "application/x-shockwave-flash"),
    /**
     * 压缩包
     */
    X_7Z("7z", "application/x-7z-compressed"),
    RAR("rar", "application/x-rar-compressed"),
    ZIP("zip", "application/zip"),
    /**
     * 音频
     */
    MP3("mp3", "audio/mpeg"),
    M4A("m4a", "audio/x-m4a"),
    /**
     * 视频
     */
    MP4("mp4", "video/mp4"),
    MPEG("mpeg", "video/mpeg"),
    MPG("mpg", "video/mpeg"),
    MOV("mov", "video/quicktime"),
    FLV("flv", "video/x-flv"),
    M4V("m4v", "video/x-m4v"),
    WMV("wmv", "video/x-ms-wmv"),
    AVI("avi", "video/x-msvideo"),
    /**
     * 文本
     */
    TXT("txt", "text/plain"),
    /**
     * 默认
     */
    DEFAULT("default", "application/octet-stream");

    /**
     * 文件类型
     */
    private final String type;
    /**
     * contentType
     */
    private final String contentType;

    ContentTypeEnum(String type, String contentType) {
        this.type = type;
        this.contentType = contentType;
    }

    public String getType() {
        return type;
    }

    public String getContentType() {
        return contentType;
    }

    /**
     * 找到contentType
     *
     * @param type
     * @return
     */
    public static String findContentType(String type) {
        if (type == null || type.isBlank()) {
            return DEFAULT.contentType;
        }
        return Arrays.stream(values())
                .filter(e -> Objects.equals(e.getType(), type.toLowerCase()))
                .findFirst()
                .map(e -> e.contentType)
                .orElse(DEFAULT.contentType);
    }

}
